package lumingweihua.future.cn.lumingweihua.ui.mine;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.zhusx.core.helper.Lib_SelectPhotoHelper;
import com.zhusx.core.interfaces.IHttpResult;
import com.zhusx.core.network.HttpRequest;
import com.zhusx.core.utils._Arrays;
import com.zhusx.core.utils._Lists;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import lumingweihua.future.cn.lumingweihua.R;
import lumingweihua.future.cn.lumingweihua.network.LoadData;
import lumingweihua.future.cn.lumingweihua.network.SimpleHttpListener;
import lumingweihua.future.cn.lumingweihua.ui.BaseCompatActivity;
import lumingweihua.future.cn.lumingweihua.ui.mine.domain.LinkNameData;
import lumingweihua.future.cn.lumingweihua.ui.mine.domain.ModifyBossData;
import lumingweihua.future.cn.lumingweihua.widget.FrescoImageView;
import lumingweihua.future.cn.lumingweihua.widget.dialog.BottomListDialog;

/**
 * todo 货主认证 第二步
 * Created by z1986 on 2018/3/17.
 */

public class ModifyGoodsBoss2Activity extends BaseCompatActivity {
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.iv_image)
    FrescoImageView ivImage;
    @BindView(R.id.iv_delete)
    ImageView ivDelete;

    OptionsPickerView startCityPickerView;
    Lib_SelectPhotoHelper helper;
    BottomListDialog dialog;
    LoadData<String> submitData;
    LoadData<List<LinkNameData>> userData;
    List<LinkNameData> nameList;
    ModifyBossData en;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_boss_author2);
        ButterKnife.bind(this);
        en = (ModifyBossData) getIntent().getSerializableExtra(_EXTRA_Serializable);
        init();
    }

    private void init() {
        helper = new Lib_SelectPhotoHelper(this);
        dialog = new BottomListDialog(this).setData(_Arrays.asList("相册", "拍照"), new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        helper._startSelectPhoto(ModifyGoodsBoss2Activity.this);
                        break;
                    case 1:
                        helper._startTakePhoto(ModifyGoodsBoss2Activity.this);
                        break;
                }
                dialog.dismiss();
            }
        });
        startCityPickerView = new OptionsPickerView.Builder(ModifyGoodsBoss2Activity.this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                tvName.setTextColor(Color.BLACK);
                tvName.setText(nameList.get(options1).linkname);
                en.profession_mobile = nameList.get(options1).mobile;
            }
        }).build();
        submitData = new LoadData<>(LoadData.Api.货主认证信息修改, this);
        submitData._setOnLoadingListener(new SimpleHttpListener<String>() {
            @Override
            public void onLoadComplete(LoadData.Api api, HttpRequest<Object> httpRequest, IHttpResult<String> result) {
                showToast(result.getData());
                setResult(RESULT_OK);
                finish();
            }
        });
        userData = new LoadData<>(LoadData.Api.个人中心新增车辆业务员列表, this);
        userData._setOnLoadingListener(new SimpleHttpListener<List<LinkNameData>>() {
            @Override
            public void onLoadComplete(LoadData.Api api, HttpRequest<Object> httpRequest, IHttpResult<List<LinkNameData>> result) {
                nameList = result.getData();
                if (_Lists.isEmpty(nameList)) {
                    showToast("暂无服务专员");
                    return;
                }
                startCityPickerView.setPicker(nameList);
            }
        });
        tvName.setTextColor(Color.BLACK);
        tvName.setText(en.profession_mobile);
        editName.setText(en.company_name);
        ivImage.setImageURI(en.permit);
    }

    @OnClick({R.id.iv_delete, R.id.tv_submit, R.id.tv_name, R.id.iv_image})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_delete:
                en.permitFile = null;
                en.permit = null;
                ivDelete.setVisibility(View.GONE);
                ivImage.setImageURI(Uri.parse("res://" + getPackageName() + "/" + R.drawable.iv_tianji));
                break;
            case R.id.tv_submit:
                if (TextUtils.isEmpty(en.profession_mobile)) {
                    showToast("请选择服务专员");
                    return;
                }
                String name = editName.getText().toString();
                if (TextUtils.isEmpty(name)) {
                    showToast("请输入企业名称");
                    return;
                }
                if (en.permitFile == null && en.permit == null) {
                    showToast("请选择企业营业执照");
                    return;
                }
                submitData._loadData(en.linkname, en.id_num, name, en.profession_mobile, en.hand_imgFile == null ? null : toBase64String(new File(en.hand_imgFile)), en.permitFile == null ? null : toBase64String(new File(en.permitFile)));
                break;
            case R.id.tv_name:
                if (userData._hasCache()) {
                    startCityPickerView.show();
                } else {
                    userData._loadData();
                }
                userData._loadData();
                break;
            case R.id.iv_image:
                if (en.permitFile != null) {
                    startActivity(new Intent(v.getContext(), PhotoActivity.class).putExtra(_EXTRA_String, Uri.fromFile(new File(en.permitFile)).toString()));
                } else if (ivImage.getVisibility() == View.VISIBLE && !TextUtils.isEmpty(en.permit)) {
                    startActivity(new Intent(v.getContext(), PhotoActivity.class).putExtra(_EXTRA_String, en.permit));
                } else {
                    dialog.show();
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        helper._onActivityResult(requestCode, resultCode, data, new Lib_SelectPhotoHelper.onDataListener() {
            @Override
            public void onPhoto(File file) {
                en.permitFile = compressFile(file).getPath();
                ivImage.setImageURI(Uri.fromFile(new File(en.permitFile)));
                ivDelete.setVisibility(View.VISIBLE);
            }
        });
    }
}
